package cn.iocoder.yudao.module.dlbz.controller.admin.suppliermoney;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dlbz.controller.admin.suppliermoney.vo.*;
import cn.iocoder.yudao.module.dlbz.dal.dataobject.suppliermoney.SupplierMoneyDO;
import cn.iocoder.yudao.module.dlbz.service.suppliermoney.SupplierMoneyService;

@Tag(name = "管理后台 - 供应商支付记录")
@RestController
@RequestMapping("/dlbz/supplier-money")
@Validated
public class SupplierMoneyController {

    @Resource
    private SupplierMoneyService supplierMoneyService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商支付记录")
    @PreAuthorize("@ss.hasPermission('dlbz:supplier-money:create')")
    public CommonResult<Long> createSupplierMoney(@Valid @RequestBody SupplierMoneySaveReqVO createReqVO) {
        return success(supplierMoneyService.createSupplierMoney(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供应商支付记录")
    @PreAuthorize("@ss.hasPermission('dlbz:supplier-money:update')")
    public CommonResult<Boolean> updateSupplierMoney(@Valid @RequestBody SupplierMoneySaveReqVO updateReqVO) {
        supplierMoneyService.updateSupplierMoney(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供应商支付记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dlbz:supplier-money:delete')")
    public CommonResult<Boolean> deleteSupplierMoney(@RequestParam("id") Long id) {
        supplierMoneyService.deleteSupplierMoney(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供应商支付记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dlbz:supplier-money:query')")
    public CommonResult<SupplierMoneyRespVO> getSupplierMoney(@RequestParam("id") Long id) {
        SupplierMoneyDO supplierMoney = supplierMoneyService.getSupplierMoney(id);
        return success(BeanUtils.toBean(supplierMoney, SupplierMoneyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应商支付记录分页")
    @PreAuthorize("@ss.hasPermission('dlbz:supplier-money:query')")
    public CommonResult<PageResult<SupplierMoneyRespVO>> getSupplierMoneyPage(@Valid SupplierMoneyPageReqVO pageReqVO) {
        PageResult<SupplierMoneyDO> pageResult = supplierMoneyService.getSupplierMoneyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SupplierMoneyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供应商支付记录 Excel")
    @PreAuthorize("@ss.hasPermission('dlbz:supplier-money:export')")
    @OperateLog(type = EXPORT)
    public void exportSupplierMoneyExcel(@Valid SupplierMoneyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SupplierMoneyDO> list = supplierMoneyService.getSupplierMoneyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供应商支付记录.xls", "数据", SupplierMoneyRespVO.class,
                        BeanUtils.toBean(list, SupplierMoneyRespVO.class));
    }

}